package stud.mpgame.flash.net.command;

import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import stud.mpgame.flash.game.Player;
import stud.mpgame.physics.Circle;
import stud.mpgame.physics.Vector2D;

import stud.mpgame.network.UserObject;
import stud.mpgame.network.Message;

import stud.mpgame.snapshot.ICommand;
import stud.mpgame.snapshot.ISnapshot;
import stud.mpgame.flash.game.GameWorld;

class ArrowKeyCommand extends UserObject, implements ICommand {
	
	private var time:Int;
	
	private var keyCode:Int;
	private var keyStatus:Bool;
    
	public function new(keyCode:Int, keyStatus:Bool, time:Int) {
		super();
		this.time = time;
		
		this.keyCode = keyCode;
		this.keyStatus = keyStatus;	
	}
	
	public override function writeExternal(output:IDataOutput, message:Message):Void {
		output.writeInt(time); //TODO REFACTOR
		output.writeInt(keyCode);
		output.writeInt(keyStatus ? 1 : 0);
	}
	
	public function getTime():Int {
		return time;
	}
	
	public function getKeyCode():Int {
		return keyCode;
	}
	
	public function iskeyPressed():Bool {
		return keyStatus;
	}
	
	//TODO
	public function apply(snapshot:ISnapshot):Void {
		var world:GameWorld = cast(snapshot, GameWorld);
		
		var localPlayer:Player = world.getLocalPlayer();
		localPlayer.applyArrowKeyCommand(this);
		
		trace("command: " + time + "; " + snapshot.getTime());
		world.addTempGameObjects(localPlayer, time);
	}
	
}
